/*
 *  远程代理模式 proxy 设置
 */
const fs = require('fs-extra')
const path = require('path')
const sfeSt = require('@sunl-fe/sfe-st')
const modifyVars = require('./antd-theme')

const target = 'http://139.155.156.20:8080' // 测试环境
const proxy = {
    '/api/zgh/*': {
        target,
        changeOrigin: true,
        onProxyReq(proxyReq, req) {
            console.info(`proxy 请求地址：${target}${req.originalUrl}`)
        },
    },
}

const overwriteLoaders = {
    file: loaders => loaders.concat([
        {
            test: /\.yml$/,
            loaders: ['json-loader', 'yaml-loader'],
        },
        {
            test: /\.styl$/,
            loaders: ['style-loader', 'css-loader?modules=true&localIdentName=[path][name]__[local]--[hash:base64:4]', 'stylus-loader'],
            exclude: { or: [/node_modules/, /global/] },
        },
        {
            test: /\.styl$/,
            loaders: ['style-loader', 'css-loader', 'stylus-loader'],
            include: [path.join(__dirname, './src/styles/global')],
        },
    ]),
}

module.exports = (env, devServerMode) => ({
    devServerPort: devServerMode === 'local' ? 8081 : 8082,
    devServerProxy: proxy,
    devServerLogin: {
        // sessionRetrieveKey: 'SESSION',
        loginUrl: 'http://172.16.116.136:9091/cas/login',
        serviceUrl: 'http://172.16.140.50:22000/workorder/',
        accounts: ['yf-yuchangchun', 'liujian02', 'liuqiao'],
    },
    alias: {
        common: path.resolve(__dirname, 'src/common/'),
        constant: path.resolve(__dirname, 'src/constants/'),
        container: path.resolve(__dirname, 'src/containers/'),
        component: path.resolve(__dirname, 'src/components/'),
        style: path.resolve(__dirname, 'src/styles/'),
        image: path.resolve(__dirname, 'src/images/'),
        'react-dom': '@hot-loader/react-dom',
    },
    devServerMockUrlMatch: /^\/api\/zgh\//,
    async beforeBuild() {
        console.info('running feboreBuild() hook')
        await fs.copy('./src/images', './dist/images')
        console.log('图片已拷贝到dist目录！')
    },
    devServerMiddleware(app) {
        // TBD项参数请确认后，作出业务相关配置；其他参数基本无需关注。
        sfeSt.server.createServer({
            app, // app为必填参数
            successFlag: 0, // 默认为1  依赖具体业务 TBD
            // needCreateMock: false, // 控制tpl => mock json脚本是否执行，默认为true 可追加文件名后缀 如'.action'  '.json' 依赖具体业务 TBD
            // mockDir: '../../../../mock', // sfe-cli mock路径 当前版本默认默认
            port: devServerMode === 'local' ? 4444 : 4445,
            // wsPath: '/sfe-st/validate', // 浏览器端发起的ws服务名
            // preparePath: filePath => filePath.replace('/testdata', '').replace('.action', ''), // 允许对ajax的url统一处理
            moduleList: [''], // 期望校验的后端服务模块名  依赖具体业务 TBD
            // passList: [], // 校验豁免列表
        })
    },
    lessLoaderModifyVars: modifyVars,
    overwriteLoaders,
})
